{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "f0dd9f04-4d8a-440f-a1e2-deb7152143e2",
   "metadata": {},
   "source": [
    "info from"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a3b06316-bff4-4c44-806d-d77e29454db4",
   "metadata": {},
   "source": [
    "https://blog.csdn.net/zhh_920509/article/details/129757516"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "772670aa-b5ea-4b29-8628-b03d3f087c97",
   "metadata": {},
   "source": [
    "AKShare Introduce"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fe2d76ae-a8e8-4c64-9362-62fdbcf0af7e",
   "metadata": {},
   "source": [
    "Gain stock data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "47de5f25-0812-4f1f-be61-219ef23314e1",
   "metadata": {},
   "outputs": [],
   "source": [
    "获取股票数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b1bcd19c-394a-4c37-a345-389b5f2672e4",
   "metadata": {},
   "outputs": [],
   "source": [
    "# General picture of the stock market\n",
    "import akshare as ak\n",
    "\n",
    "# Shanghai Stock Exchange\n",
    "# http://www.sse.com.cn/market/stockdata/statistic/\n",
    "def sh_df():\n",
    "    stock_sse_summary_df = ak.stock_sse_summary()\n",
    "    print(stock_sse_summary_df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "ab4db37e-8f0d-49c8-bf43-2e27d947b020",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Shenzhen Stock Exchange\n",
    "# 证券类别统计\n",
    "# http://www.szse.cn/market/overview/index.html\n",
    "def sz_df():\n",
    "    stock_szse_summary_df = ak.stock_szse_summary()\n",
    "    print(stock_szse_summary_df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "c02bd4df-51dd-4654-9eee-6b59ee729f0c",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Shenzhen Stock Exchange\n",
    "# 地区trade sort\n",
    "# http://www.szse.cn/market/overview/index.html\n",
    "def sz_area():\n",
    "    stock_szse_area_summary_df = ak.stock_szse_area_summary(date='202203')\n",
    "    print(stock_szse_area_summary_df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "d92d1876-1d35-4773-8d66-4159f495b8f2",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "# 深圳证券交易所\n",
    "# 股票行业成交\n",
    "# http://docs.static.szse.cn/www/market/periodical/month/W020220511355248518608.html\n",
    "def sz_sector():\n",
    "    stock_szse_sector_summary_df = ak.stock_szse_sector_summary(symbol=\"当年\", date=\"202204\")\n",
    "    print(stock_szse_sector_summary_df)\n",
    " \n",
    "# 上海证券交易所\n",
    "# 每日概况\n",
    "#  http://www.sse.com.cn/market/stockdata/overview/day/\n",
    "def sh_day():\n",
    "    stock_sse_deal_daily_df = ak.stock_sse_deal_daily(date=\"20201111\")\n",
    "    print(stock_sse_deal_daily_df)\n",
    " \n",
    " \n",
    "def get_account_statistics():\n",
    "    # 股票账户统计月度\n",
    "    \"\"\"\n",
    "    输出参数\n",
    "    名称    类型    描述\n",
    "    数据日期    object    -\n",
    "    新增投资者-数量    float64    注意单位: 万户\n",
    "    新增投资者-环比    float64    -\n",
    "    新增投资者-同比    float64    -\n",
    "    期末投资者-总量    float64    注意单位: 万户\n",
    "    期末投资者-A股账户    float64    注意单位: 万户\n",
    "    期末投资者-B股账户    float64    注意单位: 万户\n",
    "    沪深总市值    float64    -\n",
    "    沪深户均市值    float64    注意单位: 万\n",
    "    上证指数-收盘    float64    -\n",
    "    上证指数-涨跌幅    float64    -\n",
    "    \"\"\"\n",
    "    account = ak.stock_account_statistics_em()\n",
    "    account.set_index(\"数据日期\", inplace=True)  # 设置索引值\n",
    "    account.to_csv(\"stock_account_statistics.csv\")\n",
    "    print(account)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "7ae4e546-fc5e-464a-8197-01c1b8e2f80d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "         新增投资者-数量  新增投资者-环比  新增投资者-同比  期末投资者-总量  期末投资者-A股账户  期末投资者-B股账户  \\\n",
      "数据日期                                                                      \n",
      "2023-08     99.59    0.0940   -0.2007  22141.58    22082.23      238.81   \n",
      "2023-07     91.03   -0.0724   -0.1959  22042.00    21982.61      238.86   \n",
      "2023-06     98.13    0.0089   -0.2623  21950.97    21891.53      238.91   \n",
      "2023-05     97.26   -0.0292   -0.1913  21852.84    21793.33      238.96   \n",
      "2023-04    100.19   -0.4716   -0.2028  21755.58    21696.01      238.99   \n",
      "...           ...       ...       ...       ...         ...         ...   \n",
      "2015-08    136.85   -0.3320       NaN   9406.60     9331.30      239.43   \n",
      "2015-07    204.87   -0.5587       NaN   9269.75     9194.35      239.22   \n",
      "2015-06    464.22    0.1163       NaN   9064.88     8989.29      238.83   \n",
      "2015-05    415.87   -0.1641       NaN   8600.66     8524.63      237.71   \n",
      "2015-04    497.53       NaN       NaN   8184.79     8108.30      235.92   \n",
      "\n",
      "                 沪深总市值   沪深户均市值    上证指数-收盘  上证指数-涨跌幅  \n",
      "数据日期                                                  \n",
      "2023-08  808664.768312  36.6206  3119.8764   -5.2009  \n",
      "2023-07  850616.505464  38.6950  3291.0398    2.7788  \n",
      "2023-06  832614.739654  38.0336  3202.0623   -0.0781  \n",
      "2023-05  825176.131820  37.8637  3204.5644   -3.5721  \n",
      "2023-04  849540.136222  39.1565  3323.2746    1.5404  \n",
      "...                ...      ...        ...       ...  \n",
      "2015-08  438027.184176  46.9417  3205.9855  -12.4938  \n",
      "2015-07  504845.638889  54.9082  3663.7256  -14.3433  \n",
      "2015-06  584573.654037  65.0300  4277.2219   -7.2537  \n",
      "2015-05  627465.456629  73.6062  4611.7440    3.8294  \n",
      "2015-04  563491.335038  69.4956  4441.6550   18.5105  \n",
      "\n",
      "[101 rows x 10 columns]\n"
     ]
    }
   ],
   "source": [
    "\n",
    "    # sh_df()\n",
    "    # sz_df()\n",
    "    # sz_area()\n",
    "    # sz_sector()\n",
    "    # sh_day()\n",
    "    get_account_statistics()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "ec76395a-805f-4055-b88a-1ab5b6888ad5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   item           value\n",
      "0   总市值  20932259079.16\n",
      "1  流通市值  19717217550.68\n",
      "2    行业              游戏\n",
      "3  上市时间        20111028\n",
      "4  股票代码          002624\n",
      "5  股票简称            完美世界\n",
      "6   总股本    1939968404.0\n",
      "7   流通股    1827360292.0\n"
     ]
    }
   ],
   "source": [
    "#个股数据查询\n",
    "\n",
    "# http://quote.eastmoney.com/concept/sh603777.html?from=classic\n",
    " \n",
    "import akshare as ak\n",
    " \n",
    "df = ak.stock_individual_info_em(symbol=\"002624\")\n",
    "print(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "f0835a14-8fae-428e-ae3f-f1fd375a1bc0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "        序号      代码     名称     最新价    涨跌幅    涨跌额       成交量           成交额  \\\n",
      "0        1  835305   云创数据   14.65  29.99   3.38   60168.0  8.632024e+07   \n",
      "1        2  835670    数字人   13.32  23.22   2.51   52487.0  6.618102e+07   \n",
      "2        3  839493   并行科技   46.36  22.52   8.52   17026.0  7.688130e+07   \n",
      "3        4  300338   开元教育    2.75  20.09   0.46  168435.0  4.264593e+07   \n",
      "4        5  300716   泉为科技    6.65  20.04   1.11   77766.0  4.751998e+07   \n",
      "...    ...     ...    ...     ...    ...    ...       ...           ...   \n",
      "5586  5587  688105    诺唯赞   23.67  -6.66  -1.69   22684.0  5.440667e+07   \n",
      "5587  5588  002203   海亮股份    9.90  -6.78  -0.72  126708.0  1.277548e+08   \n",
      "5588  5589  688025    杰普特   51.01  -6.83  -3.74   38210.0  1.967606e+08   \n",
      "5589  5590  002032  苏 泊 尔   54.18  -7.10  -4.14   26035.0  1.437282e+08   \n",
      "5590  5591  688301   奕瑞科技  239.03 -10.13 -26.95    7965.0  1.948257e+08   \n",
      "\n",
      "         振幅      最高  ...    量比   换手率  市盈率-动态   市净率           总市值  \\\n",
      "0     12.78   14.65  ...  7.93  7.42  -70.75  2.29  1.939310e+09   \n",
      "1     24.42   13.99  ...  4.60  7.90   81.46  5.58  1.414350e+09   \n",
      "2     18.47   48.59  ...  5.25  6.81  -38.96  8.35  2.699543e+09   \n",
      "3     17.47    2.75  ...  2.01  5.15  -25.32  9.82  1.107342e+09   \n",
      "4     20.22    6.65  ...  2.39  4.86   -7.39  5.84  1.064133e+09   \n",
      "...     ...     ...  ...   ...   ...     ...   ...           ...   \n",
      "5586   7.89   25.36  ...  1.69  1.35  -92.96  2.36  9.468237e+09   \n",
      "5587   6.40   10.57  ...  1.98  0.65   15.30  1.50  1.978336e+10   \n",
      "5588  10.01   54.58  ...  2.21  4.02   43.29  2.50  4.845488e+09   \n",
      "5589   7.30   58.38  ...  2.24  0.32   24.06  7.91  4.370748e+10   \n",
      "5590   9.50  260.33  ...  1.49  0.78   42.24  6.30  2.438080e+10   \n",
      "\n",
      "              流通市值    涨速  5分钟涨跌  60日涨跌幅  年初至今涨跌幅  \n",
      "0     1.188128e+09  0.00   0.00    1.88   -21.45  \n",
      "1     8.845602e+08 -1.04  -1.70   27.22   -12.83  \n",
      "2     1.158331e+09 -0.73  -1.47  -28.01   -17.68  \n",
      "3     8.992777e+08  0.00   0.00  -41.61   -38.20  \n",
      "4     1.064133e+09  0.00   0.00  -44.54   -42.52  \n",
      "...            ...   ...    ...     ...      ...  \n",
      "5586  3.973291e+09  0.30   0.13  -35.66   -26.08  \n",
      "5587  1.916394e+10  0.00  -0.20   -4.53   -11.21  \n",
      "5588  4.845488e+09 -1.58  -2.22  -38.19   -44.88  \n",
      "5589  4.359159e+10  0.04  -0.18    9.99     2.21  \n",
      "5590  2.438080e+10 -0.71  -0.82    6.67   -26.51  \n",
      "\n",
      "[5591 rows x 23 columns]\n"
     ]
    }
   ],
   "source": [
    "# 实时行情数据-东财\n",
    "# 沪深京 A 股\n",
    "# http://quote.eastmoney.com/center/gridlist.html#hs_a_board\n",
    "import akshare as ak\n",
    " \n",
    "# 实时行情数据-东财\n",
    "# 沪深京 A 股\n",
    "# 单次返回所有沪深京 A 股上市公司的实时行情数据\n",
    "def em_spot():\n",
    "    stock_zh_a_spot_em_df = ak.stock_zh_a_spot_em()\n",
    "    print(stock_zh_a_spot_em_df)\n",
    " \n",
    " \n",
    "# 实时行情数据-东财\n",
    "# 沪 A 股\n",
    "# http://quote.eastmoney.com/center/gridlist.html#sh_a_board\n",
    "def em_sha_spot():\n",
    "    stock_sh_a_spot_em_df = ak.stock_sh_a_spot_em()\n",
    "    print(stock_sh_a_spot_em_df)\n",
    " \n",
    " \n",
    "# 实时行情数据-东财\n",
    "# 深 A 股\n",
    "# http://quote.eastmoney.com/center/gridlist.html#sz_a_board\n",
    "def em_sza_spot():\n",
    "    stock_sz_a_spot_em_df = ak.stock_sz_a_spot_em()\n",
    "    print(stock_sz_a_spot_em_df)\n",
    " \n",
    " \n",
    "# 实时行情数据-东财\n",
    "# 京 A 股\n",
    "# http://quote.eastmoney.com/center/gridlist.html#bj_a_board\n",
    "def em_bja_spot():\n",
    "    stock_bj_a_spot_em_df = ak.stock_bj_a_spot_em()\n",
    "    print(stock_bj_a_spot_em_df)\n",
    " \n",
    " \n",
    "# 实时行情数据-东财\n",
    "# 新股\n",
    "# http://quote.eastmoney.com/center/gridlist.html#newshares\n",
    "def em_new_spot():\n",
    "    stock_new_a_spot_em_df = ak.stock_new_a_spot_em()\n",
    "    print(stock_new_a_spot_em_df)\n",
    " \n",
    " \n",
    "# 实时行情数据-东财\n",
    "# 科创板\n",
    "# http://quote.eastmoney.com/center/gridlist.html#hs_a_board\n",
    "def em_kc_spot():\n",
    "    stock_kc_a_spot_em_em_df = ak.stock_kc_a_spot_em()\n",
    "    print(stock_kc_a_spot_em_em_df)\n",
    " \n",
    " \n",
    "# 实时行情数据-新浪\n",
    "# http://vip.stock.finance.sina.com.cn/mkt/#hs_a\n",
    "def xl_a_spot():\n",
    "    stock_zh_a_spot_df = ak.stock_zh_a_spot()\n",
    "    print(stock_zh_a_spot_df)\n",
    " \n",
    " \n",
    "if __name__ == \"__main__\":\n",
    "    em_spot()\n",
    "    # em_sha_spot()\n",
    "    # em_sza_spot()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "cfb32f30-efcc-46fd-b653-107b7496a50f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "             日期     开盘     收盘     最高     最低      成交量           成交额     振幅   涨跌幅   涨跌额    换手率\n",
      "0    2019-03-01  16.91  17.88  18.74  16.82   299464  8.935795e+08  11.38  5.99  1.01   6.14\n",
      "1    2019-03-08  18.41  18.14  20.54  17.88   366442  1.142173e+09  14.88  1.45  0.26   7.52\n",
      "2    2019-03-15  18.88  19.46  22.37  18.67   640174  2.149125e+09  20.40  7.28  1.32  13.13\n",
      "3    2019-03-22  19.68  21.14  21.57  19.60   398005  1.352457e+09  10.12  8.63  1.68   8.17\n",
      "4    2019-03-29  20.51  19.29  20.51  18.04   357751  1.140475e+09  11.68 -8.75 -1.85   7.34\n",
      "..          ...    ...    ...    ...    ...      ...           ...    ...   ...   ...    ...\n",
      "227  2023-08-04  14.62  15.16  15.26  14.49  1997772  2.997422e+09   5.31  4.48  0.65  10.93\n",
      "228  2023-08-11  15.20  14.39  15.53  14.38  1835501  2.743514e+09   7.59 -5.08 -0.77  10.04\n",
      "229  2023-08-18  14.23  13.55  14.65  13.55  1605162  2.263739e+09   7.64 -5.84 -0.84   8.78\n",
      "230  2023-08-25  13.53  13.00  13.83  12.92  1445416  1.944001e+09   6.72 -4.06 -0.55   7.91\n",
      "231  2023-09-01  13.79  13.93  14.19  13.26  1808269  2.487705e+09   7.15  7.15  0.93   9.90\n",
      "\n",
      "[232 rows x 11 columns]\n"
     ]
    }
   ],
   "source": [
    "# 历史行情数据-东财\n",
    "#  https://quote.eastmoney.com/concept/sh603777.html?from=classic\n",
    "import akshare as ak\n",
    "import pandas as pd\n",
    " \n",
    "pd.set_option('expand_frame_repr', False)  # True就是可以换行显示。设置成False的时候不允许换行\n",
    "pd.set_option('display.max_columns', None)  # 显示所有列\n",
    "# pd.set_option('display.max_rows', None)  # 显示所有行\n",
    "pd.set_option('colheader_justify', 'centre')  # 显示居中\n",
    " \n",
    " \n",
    "def a_hist():\n",
    "    # period\n",
    "    #     str\n",
    "    #     choice of {'daily', 'weekly', 'monthly'}\n",
    "    # start_date\n",
    "    #     str\n",
    "    #     开始查询的日期\n",
    "    # end_date\n",
    "    #     str\n",
    "    #     结束查询的日期\n",
    "    # adjust\n",
    "    #     str\n",
    "    #     默认返回不复权的数据;\n",
    "    #     qfq: 返回前复权后的数据;\n",
    "    #     hfq: 返回后复权后的数据\n",
    "    stock_zh_a_hist_df = ak.stock_zh_a_hist(symbol=\"002624\", period=\"weekly\", start_date=\"20190301\", end_date='20230907',\n",
    "                                            adjust=\"qfq\")\n",
    "    print(stock_zh_a_hist_df)\n",
    " \n",
    " \n",
    " \n",
    "# 分时数据-东财\n",
    "# http://quote.eastmoney.com/concept/sh603777.html?from=classic\n",
    "# period    str     choice of {'1', '5', '15', '30', '60'}; \n",
    "# adjust    str     choice of {'', 'qfq', 'hfq'}; '': 不复权, 'qfq': 前复权, 'hfq': 后复权,\n",
    "# 其中 1 分钟数据返回近 5 个交易日数据且不复权\n",
    "def a_hist_min():\n",
    "    stock_zh_a_hist_min_em_df = ak.stock_zh_a_hist_min_em(symbol=\"002624\", start_date=\"2023-01-01 09:30:00\", end_date=\"2023-02-03 15:00:00\", period='30', adjust='')\n",
    "    print(stock_zh_a_hist_min_em_df)\n",
    " \n",
    " \n",
    "if __name__ == \"__main__\":\n",
    "    a_hist()\n",
    "    # a_hist_min()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "839afe31-7f41-4884-a222-8c5c8b52df88",
   "metadata": {},
   "source": [
    "# 4.gain the stock data in local storage"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "a04d014b-67e0-4e43-b928-380fe6c9e477",
   "metadata": {},
   "outputs": [
    {
     "ename": "FileNotFoundError",
     "evalue": "[Errno 2] No such file or directory: 'I:\\\\akshare_stock\\\\stock_datas\\\\stock_list.csv'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mFileNotFoundError\u001b[0m                         Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[31], line 31\u001b[0m\n\u001b[0;32m     28\u001b[0m     stock_zh_a_hist_min_em_df\u001b[38;5;241m.\u001b[39mto_csv(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mI:\u001b[39m\u001b[38;5;130;01m\\\\\u001b[39;00m\u001b[38;5;124makshare_stock\u001b[39m\u001b[38;5;130;01m\\\\\u001b[39;00m\u001b[38;5;124mstock_datas\u001b[39m\u001b[38;5;130;01m\\\\\u001b[39;00m\u001b[38;5;124mminute\u001b[39m\u001b[38;5;130;01m\\\\\u001b[39;00m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;241m+\u001b[39msymbol\u001b[38;5;241m+\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m_\u001b[39m\u001b[38;5;132;01m{}\u001b[39;00m\u001b[38;5;124m.csv\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;241m.\u001b[39mformat(period))\n\u001b[0;32m     30\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;18m__name__\u001b[39m \u001b[38;5;241m==\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m__main__\u001b[39m\u001b[38;5;124m\"\u001b[39m:\n\u001b[1;32m---> 31\u001b[0m     df \u001b[38;5;241m=\u001b[39m \u001b[43mpd\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mread_csv\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mI:\u001b[39;49m\u001b[38;5;130;43;01m\\\\\u001b[39;49;00m\u001b[38;5;124;43makshare_stock\u001b[39;49m\u001b[38;5;130;43;01m\\\\\u001b[39;49;00m\u001b[38;5;124;43mstock_datas\u001b[39;49m\u001b[38;5;130;43;01m\\\\\u001b[39;49;00m\u001b[38;5;124;43mstock_list.csv\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m)\u001b[49m\n\u001b[0;32m     32\u001b[0m     df\u001b[38;5;241m.\u001b[39msort_values(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124msymbol\u001b[39m\u001b[38;5;124m\"\u001b[39m,  inplace\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mTrue\u001b[39;00m)\n\u001b[0;32m     33\u001b[0m     code \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mlist\u001b[39m(df[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mts_code\u001b[39m\u001b[38;5;124m\"\u001b[39m])\n",
      "File \u001b[1;32m~\\AppData\\Local\\miniconda3\\Lib\\site-packages\\pandas\\io\\parsers\\readers.py:948\u001b[0m, in \u001b[0;36mread_csv\u001b[1;34m(filepath_or_buffer, sep, delimiter, header, names, index_col, usecols, dtype, engine, converters, true_values, false_values, skipinitialspace, skiprows, skipfooter, nrows, na_values, keep_default_na, na_filter, verbose, skip_blank_lines, parse_dates, infer_datetime_format, keep_date_col, date_parser, date_format, dayfirst, cache_dates, iterator, chunksize, compression, thousands, decimal, lineterminator, quotechar, quoting, doublequote, escapechar, comment, encoding, encoding_errors, dialect, on_bad_lines, delim_whitespace, low_memory, memory_map, float_precision, storage_options, dtype_backend)\u001b[0m\n\u001b[0;32m    935\u001b[0m kwds_defaults \u001b[38;5;241m=\u001b[39m _refine_defaults_read(\n\u001b[0;32m    936\u001b[0m     dialect,\n\u001b[0;32m    937\u001b[0m     delimiter,\n\u001b[1;32m   (...)\u001b[0m\n\u001b[0;32m    944\u001b[0m     dtype_backend\u001b[38;5;241m=\u001b[39mdtype_backend,\n\u001b[0;32m    945\u001b[0m )\n\u001b[0;32m    946\u001b[0m kwds\u001b[38;5;241m.\u001b[39mupdate(kwds_defaults)\n\u001b[1;32m--> 948\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43m_read\u001b[49m\u001b[43m(\u001b[49m\u001b[43mfilepath_or_buffer\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mkwds\u001b[49m\u001b[43m)\u001b[49m\n",
      "File \u001b[1;32m~\\AppData\\Local\\miniconda3\\Lib\\site-packages\\pandas\\io\\parsers\\readers.py:611\u001b[0m, in \u001b[0;36m_read\u001b[1;34m(filepath_or_buffer, kwds)\u001b[0m\n\u001b[0;32m    608\u001b[0m _validate_names(kwds\u001b[38;5;241m.\u001b[39mget(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mnames\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;28;01mNone\u001b[39;00m))\n\u001b[0;32m    610\u001b[0m \u001b[38;5;66;03m# Create the parser.\u001b[39;00m\n\u001b[1;32m--> 611\u001b[0m parser \u001b[38;5;241m=\u001b[39m \u001b[43mTextFileReader\u001b[49m\u001b[43m(\u001b[49m\u001b[43mfilepath_or_buffer\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwds\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m    613\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m chunksize \u001b[38;5;129;01mor\u001b[39;00m iterator:\n\u001b[0;32m    614\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m parser\n",
      "File \u001b[1;32m~\\AppData\\Local\\miniconda3\\Lib\\site-packages\\pandas\\io\\parsers\\readers.py:1448\u001b[0m, in \u001b[0;36mTextFileReader.__init__\u001b[1;34m(self, f, engine, **kwds)\u001b[0m\n\u001b[0;32m   1445\u001b[0m     \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39moptions[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mhas_index_names\u001b[39m\u001b[38;5;124m\"\u001b[39m] \u001b[38;5;241m=\u001b[39m kwds[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mhas_index_names\u001b[39m\u001b[38;5;124m\"\u001b[39m]\n\u001b[0;32m   1447\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mhandles: IOHandles \u001b[38;5;241m|\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m\n\u001b[1;32m-> 1448\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_engine \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_make_engine\u001b[49m\u001b[43m(\u001b[49m\u001b[43mf\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mengine\u001b[49m\u001b[43m)\u001b[49m\n",
      "File \u001b[1;32m~\\AppData\\Local\\miniconda3\\Lib\\site-packages\\pandas\\io\\parsers\\readers.py:1705\u001b[0m, in \u001b[0;36mTextFileReader._make_engine\u001b[1;34m(self, f, engine)\u001b[0m\n\u001b[0;32m   1703\u001b[0m     \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mb\u001b[39m\u001b[38;5;124m\"\u001b[39m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;129;01min\u001b[39;00m mode:\n\u001b[0;32m   1704\u001b[0m         mode \u001b[38;5;241m+\u001b[39m\u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mb\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m-> 1705\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mhandles \u001b[38;5;241m=\u001b[39m \u001b[43mget_handle\u001b[49m\u001b[43m(\u001b[49m\n\u001b[0;32m   1706\u001b[0m \u001b[43m    \u001b[49m\u001b[43mf\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m   1707\u001b[0m \u001b[43m    \u001b[49m\u001b[43mmode\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m   1708\u001b[0m \u001b[43m    \u001b[49m\u001b[43mencoding\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43moptions\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mget\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mencoding\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mNone\u001b[39;49;00m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m   1709\u001b[0m \u001b[43m    \u001b[49m\u001b[43mcompression\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43moptions\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mget\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mcompression\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mNone\u001b[39;49;00m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m   1710\u001b[0m \u001b[43m    \u001b[49m\u001b[43mmemory_map\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43moptions\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mget\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mmemory_map\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mFalse\u001b[39;49;00m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m   1711\u001b[0m \u001b[43m    \u001b[49m\u001b[43mis_text\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mis_text\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m   1712\u001b[0m \u001b[43m    \u001b[49m\u001b[43merrors\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43moptions\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mget\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mencoding_errors\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mstrict\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m   1713\u001b[0m \u001b[43m    \u001b[49m\u001b[43mstorage_options\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43moptions\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mget\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mstorage_options\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mNone\u001b[39;49;00m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m   1714\u001b[0m \u001b[43m\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m   1715\u001b[0m \u001b[38;5;28;01massert\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mhandles \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m\n\u001b[0;32m   1716\u001b[0m f \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mhandles\u001b[38;5;241m.\u001b[39mhandle\n",
      "File \u001b[1;32m~\\AppData\\Local\\miniconda3\\Lib\\site-packages\\pandas\\io\\common.py:863\u001b[0m, in \u001b[0;36mget_handle\u001b[1;34m(path_or_buf, mode, encoding, compression, memory_map, is_text, errors, storage_options)\u001b[0m\n\u001b[0;32m    858\u001b[0m \u001b[38;5;28;01melif\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(handle, \u001b[38;5;28mstr\u001b[39m):\n\u001b[0;32m    859\u001b[0m     \u001b[38;5;66;03m# Check whether the filename is to be opened in binary mode.\u001b[39;00m\n\u001b[0;32m    860\u001b[0m     \u001b[38;5;66;03m# Binary mode does not support 'encoding' and 'newline'.\u001b[39;00m\n\u001b[0;32m    861\u001b[0m     \u001b[38;5;28;01mif\u001b[39;00m ioargs\u001b[38;5;241m.\u001b[39mencoding \u001b[38;5;129;01mand\u001b[39;00m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mb\u001b[39m\u001b[38;5;124m\"\u001b[39m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;129;01min\u001b[39;00m ioargs\u001b[38;5;241m.\u001b[39mmode:\n\u001b[0;32m    862\u001b[0m         \u001b[38;5;66;03m# Encoding\u001b[39;00m\n\u001b[1;32m--> 863\u001b[0m         handle \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mopen\u001b[39;49m\u001b[43m(\u001b[49m\n\u001b[0;32m    864\u001b[0m \u001b[43m            \u001b[49m\u001b[43mhandle\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    865\u001b[0m \u001b[43m            \u001b[49m\u001b[43mioargs\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mmode\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    866\u001b[0m \u001b[43m            \u001b[49m\u001b[43mencoding\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mioargs\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mencoding\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    867\u001b[0m \u001b[43m            \u001b[49m\u001b[43merrors\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43merrors\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    868\u001b[0m \u001b[43m            \u001b[49m\u001b[43mnewline\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\n\u001b[0;32m    869\u001b[0m \u001b[43m        \u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m    870\u001b[0m     \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m    871\u001b[0m         \u001b[38;5;66;03m# Binary mode\u001b[39;00m\n\u001b[0;32m    872\u001b[0m         handle \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mopen\u001b[39m(handle, ioargs\u001b[38;5;241m.\u001b[39mmode)\n",
      "\u001b[1;31mFileNotFoundError\u001b[0m: [Errno 2] No such file or directory: 'I:\\\\akshare_stock\\\\stock_datas\\\\stock_list.csv'"
     ]
    }
   ],
   "source": [
    "import akshare as ak\n",
    "import pandas as pd\n",
    " \n",
    "pd.set_option('expand_frame_repr', False)  # True就是可以换行显示。设置成False的时候不允许换行\n",
    "pd.set_option('display.max_columns', None)  # 显示所有列\n",
    "pd.set_option('display.max_rows', None)  # 显示所有行\n",
    "pd.set_option('colheader_justify', 'centre')  # 显示居中\n",
    " \n",
    " \n",
    "def download_hist(symbol=\"002624\", period=\"daily\", start=\"1990101\", end=\"20230318\", adjust=\"qfq\"):\n",
    "    stock_zh_a_hist_df = ak.stock_zh_a_hist(symbol=symbol, period=period, start_date=start, end_date=end,\n",
    "                                            adjust=adjust)\n",
    "    stock_zh_a_hist_df.sort_values(\"日期\", inplace=True)\n",
    "    stock_zh_a_hist_df.set_index(\"日期\", inplace=True)\n",
    "    if period==\"daily\":\n",
    "        stock_zh_a_hist_df.to_csv(\"I:\\\\akshare_stock\\\\stock_datas\\\\day\\\\{}.csv\".format(symbol))\n",
    "    elif period==\"weekly\":\n",
    "        stock_zh_a_hist_df.to_csv(\"I:\\\\akshare_stock\\\\stock_datas\\\\week\\\\{}.csv\".format(symbol))\n",
    "    elif period==\"monthly\":\n",
    "        stock_zh_a_hist_df.to_csv(\"I:\\\\akshare_stock\\\\stock_datas\\\\month\\\\{}.csv\".format(symbol))\n",
    "    else:\n",
    "        print(\"period错误\")\n",
    " \n",
    " \n",
    "def download_hist_min(symbol=\"002624\", start=\"1990-01-01 09:30:00\", end=\"2023-02-03 15:00:00\", period='30', adjust='qfq'):\n",
    "    stock_zh_a_hist_min_em_df = ak.stock_zh_a_hist_min_em(symbol=symbol, start_date=start, end_date=end, period=period, adjust=adjust)\n",
    "    stock_zh_a_hist_min_em_df.set_index(\"时间\", inplace=True)\n",
    "    stock_zh_a_hist_min_em_df.to_csv(\"I:\\\\akshare_stock\\\\stock_datas\\\\minute\\\\\"+symbol+\"_{}.csv\".format(period))\n",
    " \n",
    "if __name__ == \"__main__\":\n",
    "    df = pd.read_csv(\"I:\\\\akshare_stock\\\\stock_datas\\\\stock_list.csv\")\n",
    "    df.sort_values(\"symbol\",  inplace=True)\n",
    "    code = list(df[\"ts_code\"])\n",
    "    print(len(code))\n",
    "    for period in ['daily', 'weekly', 'monthly']:\n",
    "        for i in range(0, len(code)):\n",
    "            symbol =  code[i].rstrip('.SZHBJ')\n",
    "            download_hist(symbol=symbol, period=period)\n",
    "            print(symbol)\n",
    " \n",
    "    for period in ['1', '5', '15', '30', '60']:\n",
    "        for i in range(0, len(code)):\n",
    "            symbol =  code[i].rstrip('.SZHBJ')\n",
    "            download_hist_min(symbol=symbol, period=period)\n",
    "            print(symbol)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f84bf2aa-9037-4efc-9d96-b124d60a0ab0",
   "metadata": {},
   "source": [
    "# 5.put the stock data saveing in the mysql data base"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "3c8d2d52-3e5e-42bb-8ee7-2ce1a48a97e8",
   "metadata": {},
   "outputs": [
    {
     "ename": "FileNotFoundError",
     "evalue": "[Errno 2] No such file or directory: 'I:\\\\akshare_stock\\\\stock_datas\\\\day\\\\stock_list.csv'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mFileNotFoundError\u001b[0m                         Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[32], line 38\u001b[0m\n\u001b[0;32m     36\u001b[0m stockDB \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124makshare_stock\u001b[39m\u001b[38;5;124m'\u001b[39m\n\u001b[0;32m     37\u001b[0m stockList \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mstock_list\u001b[39m\u001b[38;5;124m'\u001b[39m\n\u001b[1;32m---> 38\u001b[0m \u001b[43mcreate_stock\u001b[49m\u001b[43m(\u001b[49m\u001b[43makcode\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mstockList\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mdb\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mstockDB\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mdate\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mday\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m)\u001b[49m\n\u001b[0;32m     39\u001b[0m df1 \u001b[38;5;241m=\u001b[39m read_csv(stockList)\n\u001b[0;32m     40\u001b[0m df1\u001b[38;5;241m.\u001b[39msort_values(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mts_code\u001b[39m\u001b[38;5;124m\"\u001b[39m,  inplace\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mTrue\u001b[39;00m)\n",
      "Cell \u001b[1;32mIn[32], line 16\u001b[0m, in \u001b[0;36mcreate_stock\u001b[1;34m(akcode, db, date)\u001b[0m\n\u001b[0;32m     14\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mcreate_stock\u001b[39m(akcode, db, date):\n\u001b[0;32m     15\u001b[0m     \u001b[38;5;66;03m# 读取本地CSV文件\u001b[39;00m\n\u001b[1;32m---> 16\u001b[0m     df \u001b[38;5;241m=\u001b[39m \u001b[43mpd\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mread_csv\u001b[49m\u001b[43m(\u001b[49m\n\u001b[0;32m     17\u001b[0m \u001b[43m        \u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mI:\u001b[39;49m\u001b[38;5;130;43;01m\\\\\u001b[39;49;00m\u001b[38;5;124;43makshare_stock\u001b[39;49m\u001b[38;5;130;43;01m\\\\\u001b[39;49;00m\u001b[38;5;124;43mstock_datas\u001b[39;49m\u001b[38;5;130;43;01m\\\\\u001b[39;49;00m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m \u001b[49m\u001b[38;5;241;43m+\u001b[39;49m\u001b[43m \u001b[49m\u001b[43mdate\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m+\u001b[39;49m\u001b[43m \u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;130;43;01m\\\\\u001b[39;49;00m\u001b[38;5;132;43;01m{}\u001b[39;49;00m\u001b[38;5;124;43m.csv\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mformat\u001b[49m\u001b[43m(\u001b[49m\u001b[43makcode\u001b[49m\u001b[43m)\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m     18\u001b[0m     engine \u001b[38;5;241m=\u001b[39m connect_db(db)\n\u001b[0;32m     19\u001b[0m     \u001b[38;5;66;03m# name='stocklist'全部小写否则会报错\u001b[39;00m\n",
      "File \u001b[1;32m~\\AppData\\Local\\miniconda3\\Lib\\site-packages\\pandas\\io\\parsers\\readers.py:948\u001b[0m, in \u001b[0;36mread_csv\u001b[1;34m(filepath_or_buffer, sep, delimiter, header, names, index_col, usecols, dtype, engine, converters, true_values, false_values, skipinitialspace, skiprows, skipfooter, nrows, na_values, keep_default_na, na_filter, verbose, skip_blank_lines, parse_dates, infer_datetime_format, keep_date_col, date_parser, date_format, dayfirst, cache_dates, iterator, chunksize, compression, thousands, decimal, lineterminator, quotechar, quoting, doublequote, escapechar, comment, encoding, encoding_errors, dialect, on_bad_lines, delim_whitespace, low_memory, memory_map, float_precision, storage_options, dtype_backend)\u001b[0m\n\u001b[0;32m    935\u001b[0m kwds_defaults \u001b[38;5;241m=\u001b[39m _refine_defaults_read(\n\u001b[0;32m    936\u001b[0m     dialect,\n\u001b[0;32m    937\u001b[0m     delimiter,\n\u001b[1;32m   (...)\u001b[0m\n\u001b[0;32m    944\u001b[0m     dtype_backend\u001b[38;5;241m=\u001b[39mdtype_backend,\n\u001b[0;32m    945\u001b[0m )\n\u001b[0;32m    946\u001b[0m kwds\u001b[38;5;241m.\u001b[39mupdate(kwds_defaults)\n\u001b[1;32m--> 948\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43m_read\u001b[49m\u001b[43m(\u001b[49m\u001b[43mfilepath_or_buffer\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mkwds\u001b[49m\u001b[43m)\u001b[49m\n",
      "File \u001b[1;32m~\\AppData\\Local\\miniconda3\\Lib\\site-packages\\pandas\\io\\parsers\\readers.py:611\u001b[0m, in \u001b[0;36m_read\u001b[1;34m(filepath_or_buffer, kwds)\u001b[0m\n\u001b[0;32m    608\u001b[0m _validate_names(kwds\u001b[38;5;241m.\u001b[39mget(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mnames\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;28;01mNone\u001b[39;00m))\n\u001b[0;32m    610\u001b[0m \u001b[38;5;66;03m# Create the parser.\u001b[39;00m\n\u001b[1;32m--> 611\u001b[0m parser \u001b[38;5;241m=\u001b[39m \u001b[43mTextFileReader\u001b[49m\u001b[43m(\u001b[49m\u001b[43mfilepath_or_buffer\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwds\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m    613\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m chunksize \u001b[38;5;129;01mor\u001b[39;00m iterator:\n\u001b[0;32m    614\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m parser\n",
      "File \u001b[1;32m~\\AppData\\Local\\miniconda3\\Lib\\site-packages\\pandas\\io\\parsers\\readers.py:1448\u001b[0m, in \u001b[0;36mTextFileReader.__init__\u001b[1;34m(self, f, engine, **kwds)\u001b[0m\n\u001b[0;32m   1445\u001b[0m     \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39moptions[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mhas_index_names\u001b[39m\u001b[38;5;124m\"\u001b[39m] \u001b[38;5;241m=\u001b[39m kwds[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mhas_index_names\u001b[39m\u001b[38;5;124m\"\u001b[39m]\n\u001b[0;32m   1447\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mhandles: IOHandles \u001b[38;5;241m|\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m\n\u001b[1;32m-> 1448\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_engine \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_make_engine\u001b[49m\u001b[43m(\u001b[49m\u001b[43mf\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mengine\u001b[49m\u001b[43m)\u001b[49m\n",
      "File \u001b[1;32m~\\AppData\\Local\\miniconda3\\Lib\\site-packages\\pandas\\io\\parsers\\readers.py:1705\u001b[0m, in \u001b[0;36mTextFileReader._make_engine\u001b[1;34m(self, f, engine)\u001b[0m\n\u001b[0;32m   1703\u001b[0m     \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mb\u001b[39m\u001b[38;5;124m\"\u001b[39m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;129;01min\u001b[39;00m mode:\n\u001b[0;32m   1704\u001b[0m         mode \u001b[38;5;241m+\u001b[39m\u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mb\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m-> 1705\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mhandles \u001b[38;5;241m=\u001b[39m \u001b[43mget_handle\u001b[49m\u001b[43m(\u001b[49m\n\u001b[0;32m   1706\u001b[0m \u001b[43m    \u001b[49m\u001b[43mf\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m   1707\u001b[0m \u001b[43m    \u001b[49m\u001b[43mmode\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m   1708\u001b[0m \u001b[43m    \u001b[49m\u001b[43mencoding\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43moptions\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mget\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mencoding\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mNone\u001b[39;49;00m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m   1709\u001b[0m \u001b[43m    \u001b[49m\u001b[43mcompression\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43moptions\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mget\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mcompression\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mNone\u001b[39;49;00m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m   1710\u001b[0m \u001b[43m    \u001b[49m\u001b[43mmemory_map\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43moptions\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mget\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mmemory_map\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mFalse\u001b[39;49;00m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m   1711\u001b[0m \u001b[43m    \u001b[49m\u001b[43mis_text\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mis_text\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m   1712\u001b[0m \u001b[43m    \u001b[49m\u001b[43merrors\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43moptions\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mget\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mencoding_errors\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mstrict\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m   1713\u001b[0m \u001b[43m    \u001b[49m\u001b[43mstorage_options\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43moptions\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mget\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mstorage_options\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mNone\u001b[39;49;00m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m   1714\u001b[0m \u001b[43m\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m   1715\u001b[0m \u001b[38;5;28;01massert\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mhandles \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m\n\u001b[0;32m   1716\u001b[0m f \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mhandles\u001b[38;5;241m.\u001b[39mhandle\n",
      "File \u001b[1;32m~\\AppData\\Local\\miniconda3\\Lib\\site-packages\\pandas\\io\\common.py:863\u001b[0m, in \u001b[0;36mget_handle\u001b[1;34m(path_or_buf, mode, encoding, compression, memory_map, is_text, errors, storage_options)\u001b[0m\n\u001b[0;32m    858\u001b[0m \u001b[38;5;28;01melif\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(handle, \u001b[38;5;28mstr\u001b[39m):\n\u001b[0;32m    859\u001b[0m     \u001b[38;5;66;03m# Check whether the filename is to be opened in binary mode.\u001b[39;00m\n\u001b[0;32m    860\u001b[0m     \u001b[38;5;66;03m# Binary mode does not support 'encoding' and 'newline'.\u001b[39;00m\n\u001b[0;32m    861\u001b[0m     \u001b[38;5;28;01mif\u001b[39;00m ioargs\u001b[38;5;241m.\u001b[39mencoding \u001b[38;5;129;01mand\u001b[39;00m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mb\u001b[39m\u001b[38;5;124m\"\u001b[39m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;129;01min\u001b[39;00m ioargs\u001b[38;5;241m.\u001b[39mmode:\n\u001b[0;32m    862\u001b[0m         \u001b[38;5;66;03m# Encoding\u001b[39;00m\n\u001b[1;32m--> 863\u001b[0m         handle \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mopen\u001b[39;49m\u001b[43m(\u001b[49m\n\u001b[0;32m    864\u001b[0m \u001b[43m            \u001b[49m\u001b[43mhandle\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    865\u001b[0m \u001b[43m            \u001b[49m\u001b[43mioargs\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mmode\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    866\u001b[0m \u001b[43m            \u001b[49m\u001b[43mencoding\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mioargs\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mencoding\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    867\u001b[0m \u001b[43m            \u001b[49m\u001b[43merrors\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43merrors\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    868\u001b[0m \u001b[43m            \u001b[49m\u001b[43mnewline\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\n\u001b[0;32m    869\u001b[0m \u001b[43m        \u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m    870\u001b[0m     \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m    871\u001b[0m         \u001b[38;5;66;03m# Binary mode\u001b[39;00m\n\u001b[0;32m    872\u001b[0m         handle \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mopen\u001b[39m(handle, ioargs\u001b[38;5;241m.\u001b[39mmode)\n",
      "\u001b[1;31mFileNotFoundError\u001b[0m: [Errno 2] No such file or directory: 'I:\\\\akshare_stock\\\\stock_datas\\\\day\\\\stock_list.csv'"
     ]
    }
   ],
   "source": [
    "\"\"\"\n",
    "date:20210918\n",
    "将CSV文件写入到MySQL中\n",
    "\"\"\"\n",
    "import pandas as pd\n",
    "from sqlalchemy import create_engine\n",
    " \n",
    " \n",
    "def connect_db(db):\n",
    "    engine = create_engine('mysql+pymysql://hao:671010@localhost:3306/{}?charset=utf8'.format(db))\n",
    "    return engine\n",
    " \n",
    " \n",
    "def create_stock(akcode, db, date):\n",
    "    # 读取本地CSV文件\n",
    "    df = pd.read_csv(\n",
    "        'I:\\\\akshare_stock\\\\stock_datas\\\\' + date + '\\\\{}.csv'.format(akcode))\n",
    "    engine = connect_db(db)\n",
    "    # name='stocklist'全部小写否则会报错\n",
    "    df.to_sql(name='ak_' + date + '_{}'.format(akcode), con=engine, index=False, if_exists='replace')\n",
    " \n",
    "def create_min_stock(akcode, db, date):\n",
    "    # 读取本地CSV文件\n",
    "    df = pd.read_csv(\n",
    "        'I:\\\\akshare_stock\\\\stock_datas\\\\minute\\\\'+akcode+'_{}.csv'.format(date))\n",
    "    engine = connect_db(db)\n",
    "    # name='stocklist'全部小写否则会报错\n",
    "    df.to_sql(name='ak_' + akcode + '_{}'.format(date), con=engine, index=False, if_exists='replace')\n",
    " \n",
    " \n",
    "def read_csv(code):\n",
    "    df = pd.read_csv('I:\\\\akshare_stock\\\\stock_datas\\\\{}.csv'.format(code))\n",
    "    return df\n",
    " \n",
    " \n",
    "stockDB = 'akshare_stock'\n",
    "stockList = 'stock_list'\n",
    "create_stock(akcode=stockList, db=stockDB, date=\"day\")\n",
    "df1 = read_csv(stockList)\n",
    "df1.sort_values(\"ts_code\",  inplace=True)\n",
    "li1 = list(df1['ts_code'])\n",
    "for date in [\"day\", \"week\", \"month\"]:\n",
    "    for i in range(0, len(li1)):\n",
    "        codeStock = li1[i].rstrip('.SHZBJ')\n",
    "        create_stock(akcode=codeStock, db=stockDB, date=date)\n",
    "        print(codeStock)\n",
    " \n",
    "for date in ['1', '5', '15', '30', '60']:\n",
    "    for i in range(0, len(li1)):\n",
    "        codeStock = li1[i].rstrip('.SHZBJ')\n",
    "        create_min_stock(akcode=codeStock, db=stockDB, date=date)\n",
    "        print(codeStock)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e187fac6-2c70-4166-a595-e69069e220a3",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
